package Jets.ui
{
   import flash.display.DisplayObjectContainer;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import Jets.display.JContainer;
   
   public class JComboBox extends JContainer
   {
	   private var btn:JPushButton;
	   private var _list:JList;
	   private var _arr:Array;
	   private var rederclass:Class;
	   
      public function JComboBox(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0, data:*=null, render:Class=null) 
	  {
         if(data)
         {
            this._arr = data;
         }
         this.rederclass = (render) || (JPushButton);
         super(parent,cx,cy);
      }
      
      override public function set skin(skinName:String) : void 
	  {
         this.btn.skin = skinName;
      }
      override public function get skin() : String 
	  {
         return this.btn.skin;
      }
      
      public function set offx(value:int) : void 
	  {
         this.btn.offx = value;
      }
      
      override protected function addChildren() : void 
	  {
         this.btn = new this.rederclass(this,0,0);
         _width = 100;
         _height = 20;
         this.btn.addEventListener(MouseEvent.CLICK,this.onPress);
         this.btn.setSize(_width,20);
         if((this._arr) && this._arr.length > 0)
         {
            this.btn.data = this._arr[0];
         }
         this._list = new JList(null,this.btn.x,this.btn.y + this.btn.height,this._arr,this.rederclass);
         this._list.ismenu = true;
         this._list.addEventListener(Event.CHANGE,this.onClickData);
         this._list.addEventListener(Event.SELECT,this.onselect);
         this._list.setSize(_width,100);
         this._list.y = 20;
         this.addEventListener(Event.ADDED_TO_STAGE,this.onadd);
      }
      
      protected function onselect(event:Event) : void 
	  {
         this.data = this._list.seldata;
         this.dispatchEvent(event);
      }
      
      protected function onadd(event:Event) : void 
	  {
         this.removeEventListener(Event.ADDED_TO_STAGE,this.onadd);
         stage.addEventListener(MouseEvent.MOUSE_UP,this.onMouseUp);
      }
      
      public function get items() : * 
	  {
         return this._list.data;
      }
      
      public function set items(data:*) : void 
	  {
         if(data)
         {
            this._list.data = data;
         }
         this._list.invalidate();
         if(data.length > 0)
         {
            if(data[0].hasOwnProperty("text"))
            {
               this.selByLabel(data[0].text);
            }
            else
            {
               this.selByLabel(data[0]);
            }
         }
         else
         {
            this.btn.data = "";
         }
      }
      
      override public function draw() : void 
	  {
         this.btn.setSize(this.width,this.height);
         this.dropdownWidth = this.width;
         this._list.draw();
      }
      
      public function selByLabel(text:String) : void 
	  {
         var i:int = 0;
         while(i < this.items.length)
         {
            if(this.items[i] == text || (this.items[i].hasOwnProperty("text")) && (this.items[i].text == text))
            {
               this.list.selindex = i;
               this.data = this.list.seldata;
               return;
            }
            i++;
         }
      }
      
      public function get selectedIndex() : int 
	  {
         return this.list.selindex;
      }
      
      public function set selectedIndex(index:int) : void 
	  {
         if(index < this.list.data.length)
         {
            this.list.selindex = index;
         }
         else
         {
            this.list.selindex = -1;
         }
      }
      
      private function onMouseUp(param1:Event) : void 
	  {
         if(!((stage) && (this.hitTestPoint(stage.mouseX,stage.mouseY,false))))
         {
            if(this._list.stage)
            {
               this.removeChild(this._list);
            }
         }
      }
      
      private function onPress(param1:Event) : void 
	  {
         if(this._list.stage)
         {
            this.removeChild(this._list);
         }
         else
         {
            this.addChild(this._list);
         }
      }
      
      private function onClickData(param1:Event) : void 
	  {
         if(this.data != this.list.seldata)
         {
            this.data = (this.list.seldata) || ("");
            this.dispatchEvent(new Event(Event.CHANGE));
         }
      }
      
      public function get list() : JList 
	  {
         return this._list;
      }
      
      public function get dropdownWidth() : Number 
	  {
         return this._list.width;
      }
      
      public function set dropdownWidth(param1:Number) : void 
	  {
         this._list.width = param1;
      }
      
      public function get dropdownHeight() : Number 
	  {
         return this._list.height;
      }
      
      public function set dropdownHeight(param1:Number) : void 
	  {
         this._list.height = param1;
      }
      
      override public function set data(param1:Object) : void 
	  {
         this.btn.data = param1;
      }
      
      override public function get data() : Object 
	  {
         return this.btn.data;
      }
   }
}
